;(function($){
	$.fn.extend({
		"createJDMenu":function(url, prefix){
			prefix = prefix||"/meetings";
			url = prefix + url;
			var self = $(this);
			$(function() {
				$.post(url, {}, function(json){
					var menus = [];
					// id, name, parent, entry
					//{
					//	page: ${pageNo},
					//	total: ${totalCount},
					//	rows: [{
					//		id: ${id},
					//		cell:[id, name, parent, entry]
					//	},...
					//	]
					//}		
					for(var i=0; i<json.rows.length; i++){
						var row = json.rows[i].cell;
						menus.push({
							id:row[0], 
							name:row[1], 
							parent:row[2], 
							entry:row[3],
							subs:[],
							toString: function(){
								return "MenuItem["+this.id+"],p="+this.parent+", name="+this.name+", entry="+this.entry+(this.subs.length>0?(", subs=["+this.subs+"]"):"");
							}
						});
					}
					var oMenus = [];
					for(var i=0; i<menus.length; i++){
						if(!menus[i].parent){
							oMenus.push(menus[i]);
						}else{
							for(var j=0; j<menus.length; j++){
								if(i!=j&&menus[j].id==menus[i].parent){
									menus[j].subs.push(menus[i]);
								}
							}
						}
					}
					self.each(function(i, ul){
						ul = $(ul);
						createMenus(ul, oMenus, true);
						$(ul).jdMenu();
					});
					function createMenus(ul, menus, first){
						var count = 0;
						for(var i=0; i<menus.length; i++){
							var menu = menus[i];
							if(menu.subs.length==0&&!menu.entry){
								continue;
							}
							var li = document.createElement('li');
							li = $(li);
							var anchor = document.createElement('a');
							if(menu.subs.length==0){
								$(anchor).html(menu.name);
								anchor.href = prefix+menu.entry;
								anchor.target = "_iframe";
								li.append(anchor);
								ul.append(li);
								count++;
							} else {
								anchor.href = "#";
								$(anchor).html(menu.name+"&raquo;");
								if(first){
									$(anchor).addClass("mr20");
								}
								li.append(anchor);
								var subs = document.createElement('ul');
								subs = $(subs);
								subs.addClass("more_list");
								var c = createMenus(subs, menu.subs);
								if(c>0){
									li.append(subs);
									ul.append(li);
									count++;
								}
							}
						}
						return count;
					}
				}, "json");

			});
		}
	});
})(jQuery);

